 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=225 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:37 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Modernize old code
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Modernize old code</div>

<div class='main-body'>
 
<br>
Java has been around for some time. There is a large amount of code that has been written for older versions of the JDK.
Such code will still run, but there are advantages to modernizing such code, when you get the chance, to take advantage 
of more modern libraries and language features.

<P>
In particular, the Java 5 release (JDK 1.5) marked a significant change, and noticeably altered (and improved) the character of typical Java code. 
When updating old code to more recent versions of Java, it's helpful to use a quick checklist of things to look out for.
Here's a list of such items related to the Java 5 release :

<P><b>Replace <tt><a href='http://java.sun.com/javase/6/docs/api/java/lang/StringBuffer.html'>StringBuffer</a></tt> with <tt><a href='http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html'>StringBuilder</a></tt></b>
<br>The older <tt>StringBuffer</a></tt> class is thread-safe, while the new <tt>StringBuilder</tt> is not.
However, it's almost always used in a context in which thread safety is superfluous. 
Hence, the extra cost of synchronization is paid without benefit. 
Although the performance improvement in moving from <tt>StringBuffer</tt> to <tt>StringBuilder</tt> may only be very slight (or perhaps not even measurable), it's generally considered better form to prefer <tt>StringBuilder</tt>.

<P><b>Use sequence parameters when appropriate</b>
<br>Sequence parameters (<a href='http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html'>varargs</a>) let you replace <tt>Object[]</tt> parameters (containing 0..N items) appearing at the <em>end</em> of a parameter list with an alternate form more convenient for the caller.
For example, 
<PRE>  public static void main(String[] aArgs){}</PRE>
can now be replaced with :
<PRE>  public static void main(String... aArgs){}</PRE>

<P><b>Replace constants with enumerations</b>
<br>Using <tt>public static final</tt> constants to represent sets of related items should be avoided in favor of the <a href='TopicAction2161-2.html?Id=1'>enumeration classes</a> now supported by Java.
In addition, you should consider replacing any "<a href='TopicAction2161-2.html?Id=1#Legacy'>roll-your-own</a>" implementations of type-safe enumerations with the new language construct.

<P><b>Use @Override liberally</b>
<br>The <tt>@Override</tt> standard annotation identifies methods that override a superclass method. 
It should be <a href='TopicAction4b04-2.html?Id=223'>used liberally</a> to indicate your intent to override.

<P><b>Avoid raw types</b>
<br>Raw types should almost always <a href='TopicActionb2d5-2.html?Id=224'>be avoided</a> in favor of parameterized types.

<P><b>Use for-each loops</b>
<br>The <a href='TopicAction23e0-2.html?Id=196'>enhanced for loop</a> (also called the for-each loop) should be used whenever available.
It is more compact, concise, and clear.

<P><b>Be careful with Comparable</b>
<br>The <a href='http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html'><tt>Comparable</tt></a> interface has been made generic.
For example, 
<PRE>  class Anatomy implements Comparable{
    public int compareTo(Object aThat){}
  }</PRE>
should now be replaced with :
<PRE>  class Anatomy implements Comparable&lt;Anatomy&gt;{
    public int compareTo(Anatomy aThat){}
  }

</PRE>

<P><b>Example</b>
<P>Here is an example of code written using Java 5 features :
<br>
<PRE>

<span class='keyword'>import</span> java.util.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> Office {

  <span class='comment'>/** 
  * Use sequence parameter (varargs) for main method.
  * 
  * Use a sequence parameter whenever array parameter appears at 
  * the END of the parameter list, and represents 0..N items.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main(String... aArgs){
    <span class='comment'>//Use parameterized type 'List&lt;String&gt;', not the raw type 'List'
</span>    List&lt;String&gt; employees = Arrays.asList(<span class='literal'>"Tom"</span>, <span class='literal'>"Fiorella"</span>, <span class='literal'>"Pedro"</span>);
    Office office = <span class='keyword'>new</span> Office(AirConditioning.OFF, employees);    
    System.out.println(office);
    
    <span class='comment'>//prefer the for-each style of loop
</span>    <span class='keyword'>for</span>(String workingStiff: employees){
      System.out.println(workingStiff);
    }
  }

  <span class='comment'>/**
  * Preferred : use enumerations, not int or String constants.
  */</span>
  <span class='keyword'>enum</span> AirConditioning {OFF, LOW, MEDIUM, HIGH}

  <span class='comment'>/*
  * Definitely NOT the preferred style :
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> OFF = <span class='literal'>1</span>;
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> LOW = <span class='literal'>2</span>;
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> MEDIUM = <span class='literal'>3</span>;
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> HIGH = <span class='literal'>4</span>;
  
  Office(AirConditioning aAirConditioning, List&lt;String&gt; aEmployees){
    fAirConditioning = aAirConditioning;
    fEmployees = aEmployees; <span class='comment'>//(no defensive copy here)
</span>  }
  
  AirConditioning getAirConditioning(){
    <span class='keyword'>return</span> fAirConditioning;
  }
  
  List&lt;String&gt; getEmployees(){
    <span class='keyword'>return</span> fEmployees; 
  }

  <span class='comment'>/*
  * Get used to typing @Override for toString, equals, and hashCode :
  */</span>
  
  <span class='keyword'>@Override</span> <span class='keyword'>public</span> String toString(){
    <span class='comment'>//..elided
</span>  }
  
  <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>boolean</span> equals(Object aThat){
    <span class='comment'>//..elided
</span>  }
  
  <span class='keyword'>@Override</span> <span class='keyword'>public</span> <span class='keyword'>int</span> hashCode(){
    <span class='comment'>//..elided
</span>  }
  
  <span class='comment'>// PRIVATE //
</span>  <span class='keyword'>private</span> <span class='keyword'>final</span> List&lt;String&gt; fEmployees;
  <span class='keyword'>private</span> <span class='keyword'>final</span> AirConditioning fAirConditioning;
} 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction2161-2.html?Id=1'>Type-Safe Enumerations</a> <br>
 
  
  <a href='TopicAction3eed-2.html?Id=10'>Implementing compareTo</a> <br>
 
  
  <a href='TopicActionc857-2.html?Id=41'>Know the core libraries</a> <br>
 
  
  <a href='TopicAction5f93-2.html?Id=119'>Overloading can be tricky</a> <br>
 
  
  <a href='TopicAction23e0-2.html?Id=196'>Use for-each liberally</a> <br>
 
  
  <a href='TopicAction4b04-2.html?Id=223'>Use @Override liberally</a> <br>
 
  
  <a href='TopicActionb2d5-2.html?Id=224'>Avoid raw types</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='225'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=225 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:37 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
